package org.jrad.util;

import java.io.File;

import org.jrad.model.PersistenceUnitData;

public class DAOHelper {

	public static String createPersistenceUnitData(PersistenceUnitData pud){
		String puPath = "template"+File.separator+"persistence.xml";
		String fileInputData = "";
		try {
			fileInputData = JRadHelper.readFile(puPath);
			fileInputData = fileInputData.replaceFirst("#persistence_unit_name#" , pud.getName());
			fileInputData = fileInputData.replaceFirst("#url#" , pud.getUrl());
			fileInputData = fileInputData.replaceFirst("#user#" , pud.getUser());
			fileInputData = fileInputData.replaceFirst("#password#" , pud.getPassword());
			fileInputData = fileInputData.replaceFirst("#driver#" , pud.getDriver());
			fileInputData = fileInputData.replaceFirst("#provider_name#" , pud.getProvider());
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fileInputData;
	}
	
	public static String createDAO(String packageRoot){
		String puPath = "template"+File.separator+"GenericDAO.java";
		String fileInputData = "";
		try {
			fileInputData = JRadHelper.readFile(puPath);
			fileInputData = fileInputData.replaceFirst("<packageRoot>" , packageRoot);			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fileInputData;
	}
	
	public static String createConnection(String packageRoot, String persistenceUnitName){
		String puPath = "template"+File.separator+"Connection.java";
		String fileInputData = "";
		try {
			fileInputData = JRadHelper.readFile(puPath);
			fileInputData = fileInputData.replaceFirst("<packageRoot>" , packageRoot);			
			fileInputData = fileInputData.replaceFirst("<persistenceUnitName>" , persistenceUnitName);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return fileInputData;
	}
}
